﻿Imports System.Text
Imports Mysoft.Map.Data
Imports Mysoft.Map.Excel

Partial Public Class Function_Filehelp_Import
    Inherits Mysoft.Map.Application.Security.AppPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            hidApplication.Value = Request.QueryString("application")
            hidFunctionCode.Value = Request.QueryString("functionCode")
        End If
    End Sub

    Protected Sub btnImport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnImport.Click

        Dim filePath As String = "\TempFiles\"
        Dim fileName As String

        Dim ext As String = fileImport.PostedFile.FileName.Substring(fileImport.PostedFile.FileName.LastIndexOf("."))

        Randomize()
        fileName = CInt(Int(&H7FFFFFFF * Rnd()) + 1).ToString & ext
        Do While IO.File.Exists(Server.MapPath(filePath) + fileName)
            fileName = CInt(Int(&H7FFFFFFF * Rnd()) + 1).ToString & ext
        Loop
        fileImport.PostedFile.SaveAs(Server.MapPath(filePath) + fileName)

        Try
            Dim wb As ExcelWookbook = New ExcelWookbook(Server.MapPath(filePath) + fileName)
            Dim ws As ExcelWorksheet
            Try
                ws = wb.Worksheets("MyFileHelp")
            Catch ex As Exception
                SetResult(False, "导入Excel文件中不包含名为MyFileHelp的标签页！")
                Return
            End Try
            If (ws.Cells(0, 0).Value <> "应用系统" Or ws.Cells(0, 1).Value <> "所属模块" Or ws.Cells(0, 2).Value <> "页面地址" Or ws.Cells(0, 3).Value <> "帮助地址" Or ws.Cells(0, 4).Value <> "页面描述") Then
                SetResult(False, "导入Excel文件中MyFileHelp标签页前五列列头必须为""应用系统""、""所属模块""、""页面地址""、""帮助地址""、""页面描述""！")
                Return
            End If
            Dim sb As StringBuilder = New StringBuilder("")
            Dim i As Integer
            For i = 1 To ws.Rows.Count - 1
                sb.AppendFormat("EXECUTE [usp_myAddFileHelp] '{0}','{1}','{2}','{3}','{4}';", ws.Cells(i, 0).Value, ws.Cells(i, 1).Value, ws.Cells(i, 2).Value, ws.Cells(i, 3).Value, ws.Cells(i, 4).Value)
            Next
            If (sb.Length > 0) Then
                MyDB.ExecSQL(sb.ToString())
            End If
            Mysoft.Map.Caching.MyCache.Remove("MyFileHelpUrlCache")
            SetResult(True, "导入Excel文件成功！")
        Catch ex As Exception
            SetResult(False, "导入Excel文件失败！")
        Finally
            IO.File.Delete(Server.MapPath(filePath) + fileName)
        End Try

    End Sub


    Private Sub SetResult(ByVal result As Boolean, ByVal message As String)
        txtResult.Text = result.ToString()
        txtMessage.Text = message
    End Sub

End Class

